#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    char buf[64]={'\0'};
    int fd = open("/dev/input/mouse0",O_RDONLY);
    if (fd<0)
    {
        perror("open err\n");
        return -1;
    }

    fd_set rdfds;

    int maxfd = fd;

    struct timeval tv;

    while (1)
    {
        tv.tv_sec=3;
        tv.tv_usec=0;
        //清除表
        FD_ZERO(&rdfds);
        //把标准输入加入到表
        FD_SET(0,&rdfds);
        //把鼠标加入到表
        FD_SET(fd,&rdfds);

        int ret = select(maxfd+1,&rdfds,NULL,NULL,&tv);
        if (ret<0)
        {
            perror("select err\n");
            continue;
        }
        else if (ret>0)//某个设备有数据了
        {
            if (FD_ISSET(0,&rdfds))
            {
                fgets(buf,64,stdin);
                printf("buf = %s\n",buf);
            }

            if (FD_ISSET(fd,&rdfds))
            {
                int len = read(fd,buf,64);
                printf("read mouse len = %d\n",len);
            }
               
        }
        else
        {
            printf("time out\n");
        }
    }
    
    close(fd);
    fd=-1;

    return 0;
}
